package com.wxzz.elearing.evaluation.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.evaluation.dto.*;
import com.wxzz.elearing.evaluation.entity.*;
import com.wxzz.elearing.evaluation.service.IBusinessCategoryRuleService;
import com.wxzz.elearing.evaluation.service.ITopicService;
import com.wxzz.elearing.evaluation.service.openfeign.IEnterpriseApiFeign;
import com.wxzz.elearning.common.utils.OpenFeignUtil;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 工厂体检/星级认证 前端控制器
 * </p>
 *
 * @author
 * @since 2021-04-10
 */
@RestController
@RequestMapping("/business-category-rule")
@Api(tags = "工厂体检/星级认证接口(总后台)")
public class BusinessCategoryRuleController {

    @Autowired
    private IBusinessCategoryRuleService businessCategoryRuleService;

    @ApiOperation(value = " 添加工厂体检(总后台)", notes = " 添加工厂体检(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "saveBusinessCategoryRule", method = RequestMethod.POST)
    public ReturnVO saveBusinessCategoryRule(@Valid @RequestBody BusinessCategoryRule businessCategoryRule) {
        return businessCategoryRuleService.saveBusinessCategoryRule(businessCategoryRule);
    }

    @ApiOperation(value = " 根据id删除工厂体检/星级认证(总后台)", notes = "根据id删除工厂体检/星级认证(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "deleteBusinessCategoryRuleById/{businessCategoryRuleId}", method = RequestMethod.POST)
    public ReturnVO deleteBusinessCategoryRuleById(@PathVariable Long businessCategoryRuleId) {
        return businessCategoryRuleService.deleteBusinessCategoryRuleById(businessCategoryRuleId);
    }

    @ApiOperation(value = " 添加星级认证(总后台)", notes = " 添加星级认证(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "saveBusinessCategoryRule4start", method = RequestMethod.POST)
    public ReturnVO saveBusinessCategoryRule4start(@Valid @RequestBody BusinessCategoryRule businessCategoryRule) {
        return businessCategoryRuleService.saveBusinessCategoryRule4start(businessCategoryRule);
    }

    @ApiOperation(value = " 工厂体检列表(总后台)", notes = " 工厂体检列表(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listBusinessCategoryRule", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryRule>>> listBusinessCategoryRule(@RequestBody BusinessCategoryRuleDto businessCategoryRuleDto) {
        return businessCategoryRuleService.listBusinessCategoryRule(0, businessCategoryRuleDto);
    }

    @Autowired
    private IEnterpriseApiFeign enterpriseApiFeign;

    @ApiOperation(value = " 企业工厂体检列表(后台)", notes = " 企业工厂体检列表(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listBusinessCategoryRule4enterprise", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryRule>>> listBusinessCategoryRule4enterprise(@RequestHeader String token, @RequestBody BusinessCategoryRuleDto businessCategoryRuleDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        ReturnVO<String> stringReturnVO = enterpriseApiFeign.selectEnterpriseById(jsonObject.getLong("enterpriseId"));
        JSONObject returnJsonObject = OpenFeignUtil.getJSONObject(stringReturnVO);
        businessCategoryRuleDto.setBusinessCategoryId(returnJsonObject.getLong("businessCategoryId"));
        return businessCategoryRuleService.listBusinessCategoryRule(0, businessCategoryRuleDto);
    }

    @ApiOperation(value = " 星级认证列表(总后台)", notes = " 星级认证列表(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listBusinessCategoryStart", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryRule>>> listBusinessCategoryStart(@RequestBody BusinessCategoryRuleDto businessCategoryRuleDto) {
        return businessCategoryRuleService.listBusinessCategoryRule(1, businessCategoryRuleDto);
    }

    @ApiOperation(value = " 企业星级认证列表(后台)", notes = " 企业星级认证列表(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listBusinessCategoryStart4enterprise", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryRule>>> listBusinessCategoryStart4enterprise(@RequestHeader String token, @RequestBody BusinessCategoryRuleDto businessCategoryRuleDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        ReturnVO<String> stringReturnVO = enterpriseApiFeign.selectEnterpriseById(jsonObject.getLong("enterpriseId"));
        JSONObject returnJsonObject = OpenFeignUtil.getJSONObject(stringReturnVO);
        businessCategoryRuleDto.setBusinessCategoryId(returnJsonObject.getLong("businessCategoryId"));
        return businessCategoryRuleService.listBusinessCategoryRule(1, businessCategoryRuleDto);
    }

    @ApiOperation(value = " 工厂体检添加题库(总后台)", notes = " 工厂体检添加题库(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "saveBusinessCategoryTopic4factory", method = RequestMethod.POST)
    public ReturnVO saveBusinessCategoryTopic4factory(@Valid @RequestBody BusinessCategoryTopicInsDto businessCategoryTopicInsDto) {
        return businessCategoryRuleService.saveBusinessCategoryTopic(0, businessCategoryTopicInsDto);
    }

    @ApiOperation(value = " 星级认证添加题库(总后台)", notes = " 星级认证添加题库(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "saveBusinessCategoryTopic4start", method = RequestMethod.POST)
    public ReturnVO saveBusinessCategoryTopic4start(@Valid @RequestBody BusinessCategoryTopicInsDto businessCategoryTopicInsDto) {
        return businessCategoryRuleService.saveBusinessCategoryTopic(1, businessCategoryTopicInsDto);
    }

    @ApiOperation(value = "列出工厂体检/星级认证记录(总后台)", notes = "列出工厂体检/星级认证记录(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listBusinessCategoryRuleHistory", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryRuleHistory>>> listBusinessCategoryRuleHistory(@Valid @RequestBody BusinessCategoryRuleHistoryDto businessCategoryRuleHistoryDto) {
        return businessCategoryRuleService.listBusinessCategoryRuleHistory(businessCategoryRuleHistoryDto);
    }

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "企业列出工厂体检/星级认证记录(总后台)", notes = "企业列出工厂体检/星级认证记录(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listBusinessCategoryRuleHistory4enterprise", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryRuleHistory>>> listBusinessCategoryRuleHistory4enterprise(@RequestHeader String token, @Valid @RequestBody BusinessCategoryRuleHistoryDto businessCategoryRuleHistoryDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        businessCategoryRuleHistoryDto.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        return businessCategoryRuleService.listBusinessCategoryRuleHistory(businessCategoryRuleHistoryDto);
    }

    @Autowired
    private ITopicService topicService;

    @ApiOperation(value = "工厂体检详情题库(总后台)", notes = "工厂体检详情题库(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listInTopic4factory", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Topic>>> listInTopic4factory(@RequestBody InTopicDto inTopicDto) {
        return topicService.listInTopic(inTopicDto);
    }

    @ApiOperation(value = "星级认证详情题库(总后台)", notes = " 星级认证详情题库(总后台)", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listInTopic4start", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Topic>>> listInTopic4start(@RequestBody InTopicDto inTopicDto) {
        return topicService.listInTopic(inTopicDto);
    }

    @ApiOperation(value = "添加或修改星级认证班组(后台)", notes = " 添加或修改星级认证班组(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveBusinessCategoryRuleGroup", method = RequestMethod.POST)
    public ReturnVO saveBusinessCategoryRuleGroup(@RequestHeader String token, @Valid @RequestBody BusinessCategoryRuleGroupInsDto businessCategoryRuleGroupInsDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryRuleService.saveBusinessCategoryRuleGroup(jsonObject.getLong("enterpriseId"), businessCategoryRuleGroupInsDto);
    }

    @ApiOperation(value = "列出星级认证班组(后台)", notes = " 列出星级认证班组(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listBusinessCategoryRuleGroup", method = RequestMethod.POST)
    public ReturnVO listBusinessCategoryRuleGroup(@RequestHeader String token, @Valid @RequestBody BusinessCategoryRuleGroupDto businessCategoryRuleGroupDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryRuleService.listBusinessCategoryRuleGroup(jsonObject.getLong("enterpriseId"), businessCategoryRuleGroupDto);
    }


    @ApiOperation(value = "根据星级班组id查询绑定的人员(后台)", notes = " 根据星级班组id查询绑定的人员(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectWxUserBusinessCategoryGroupByBusinessCategoryRuleGroupId", method = RequestMethod.POST)
    public ReturnVO selectWxUserBusinessCategoryGroupByBusinessCategoryRuleGroupId(@RequestHeader String token, @Valid @RequestBody WxUserBusinessCategoryGroupDto wxUserBusinessCategoryGroupDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryRuleService.selectWxUserBusinessCategoryGroupByBusinessCategoryRuleGroupId(jsonObject.getLong("enterpriseId"), wxUserBusinessCategoryGroupDto);
    }

    @ApiOperation(value = "分配工厂体检人员(后台)", notes = "分配工厂体检人员(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveWxUserBusinessCategoryGroup4factory", method = RequestMethod.POST)
    public ReturnVO saveWxUserBusinessCategoryGroup4factory(@RequestHeader String token, @Valid @RequestBody WxUserBusinessCategoryGroup4factoryDto wxUserBusinessCategoryGroup4factoryDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryRuleService.saveWxUserBusinessCategoryGroup4factory(jsonObject.getLong("enterpriseId"), wxUserBusinessCategoryGroup4factoryDto);
    }

    @ApiOperation(value = "查询工厂体检绑定人员(后台)", notes = " 查询工厂体检绑定人员(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectFactoryBind", method = RequestMethod.POST)
    public ReturnVO selectFactoryBind(@RequestHeader String token, @Valid @RequestBody SelectFactoryBindDto selectFactoryBindDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryRuleService.selectFactoryBind(jsonObject.getLong("enterpriseId"), selectFactoryBindDto);
    }

    @ApiOperation(value = "查询该企业的班组英雄榜(后台)", notes = " 查询该企业的班组英雄榜(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectBusinessCategoryRuleGroupStatisticsDataByGroup", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<BusinessCategoryRuleGroupStatisticsData>>> selectBusinessCategoryRuleGroupStatisticsDataByGroup(HttpServletResponse response, @RequestHeader String token, @RequestBody BusinessCategoryRuleGroupStatisticsDataByGroupDto businessCategoryRuleGroupStatisticsDataByGroupDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return businessCategoryRuleService.selectBusinessCategoryRuleGroupStatisticsDataByGroup(response, jsonObject.getLong("enterpriseId"), businessCategoryRuleGroupStatisticsDataByGroupDto);
    }

}
